﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Dictionary<QualificationLevel, List<Student>>>" %>
    <%if (ViewData["LoggedUser"] is SystemManager){ %>
    <script type="text/javascript">
        function completionChanged(checker) {
            var hiddenField = $(checker).parents("td:first").find('input[type="hidden"][name!=IsCompleted]');
            var path = '<%:Url.RouteUrl("Default", new {controller = "GlobalTasksCompletions", action = "SetCompletion"})%>?' + 'globalTaskId=' + $(hiddenField).attr('globalTaskId') + '&studentId=' + $(hiddenField).attr('studentId') + '&isCompleted=' + $(checker).attr('checked');
            $.ajax({
                type: "GET",
                url: path,
                success: function (data) {
                    path = '<%:Url.RouteUrl("Default", new {controller = "GlobalTasksCompletions", action = "Get"})%>?';
                    $(data).each(function () {
                        var jsonData = this;
                        $.ajax({
                            url: path + 'globalTaskId=' + jsonData.GlobalTaskId + '&studentId=' + jsonData.StudentId,
                            success: function (data, textStatus, jqXHR) {
                               $('input[type="hidden"][globalTaskId="' + jsonData.GlobalTaskId + '"][studentId="' + jsonData.StudentId + '"]').parents("td").replaceWith(data);
                            }
                        });
                    });
                }
            });
        }

        function showCanNotEditDialog(checker) {
            alert('Необходимо завершить предыдущие задание');
        }

        function taskCompletionChanged(checker) {

            var hiddenField = $(checker).parents("li:first").find('input[type="hidden"][name!=IsCompleted]');     
            var path = '<%:Url.RouteUrl("Default", new {controller = "Tasks", action = "SetCompletion"})%>?' + 'taskId=' + $(hiddenField).attr('taskId') + '&isCompleted=' + $(checker).attr('checked');
            $.ajax({
                type: "GET",
                url: path,
                success: function (data) {
                    try {
                        jQuery.parseJSON(data);
                    }
                    catch (exception_var) {
                        $(checker).attr('checked', false);
                    }

                    path = '<%:Url.RouteUrl("Default", new {controller = "Students", action = "StudentsTasks"})%>?';
                    $(data).each(function () {
                        var jsonData = this;
                        $.ajax({
                            url: path + 'studentId=' + jsonData.StudentId + '&globalTaskId=' + jsonData.GlobalTaskId,
                            success: function (data, textStatus, jqXHR) {
                                var td = $('input[type="hidden"][taskid="' + jsonData.TaskId + '"]').parents("td:first");
                                $(td).html(data);

                                $(".collapserHead", td).collapser({
                                    target: 'next',
                                    effect: 'slide',
                                    changeText: 0,
                                    expandClass: 'collapserExpand',
                                    collapseClass: 'collapserCollapse'
                                }, function () {

                                }, function () {

                                });

                            }
                        });
                    });
                }
            });
        }

        $(function () {
            $(".CompletionSetter").change(function () {

            });
            
        });
    </script>
    <%} %>

    <script type="text/javascript">
        $(function () {
            $(".collapserHead").collapser({
                target: 'next',
                effect: 'slide',
                changeText: 0,
                expandClass: 'collapserExpand',
                collapseClass: 'collapserCollapse'
            }, function () {

            }, function () {

            });
        });

    </script>
   <%
       foreach (var level in Model)
       {%>
       <h2><%:level.Key.ToString()%></h2>
       <div style="width: 100%; overflow: auto; padding: 8px;">
       <table id="gtStatuses">
       <tr><td rowspan='2'></td><%
           foreach (PlaningType type in (IEnumerable<PlaningType>) ViewData["PlaningTypes"])
           {%><%
               int globalTasksCount =
                   type.GlobalTasks.Where(gt => gt.QualificationLevel == level.Key && gt.DeletionDate == null).Count();%><td colspan='<%:(2*globalTasksCount).ToString()%>' class="planingType"><%:type.ToString()%></td><%
           }%></tr>
       <tr><%
           foreach (PlaningType type in (IEnumerable<PlaningType>) ViewData["PlaningTypes"])
           {%>
                <%
               foreach (
                   GlobalTask globalTask in
                       type.GlobalTasks.Where(gt => gt.QualificationLevel == level.Key && gt.DeletionDate == null).
                           OrderBy(gt => gt.StartDate))
               {%>
                    
                        <td>
                        ...
                        </td>
                  
                    
                        <td class="globalTaskName"><%:globalTask.Name%></td>
                <%
               }%>
       <%
           }%></tr>
       
        <%
           foreach (Student student in level.Value) 
           {%>
           <tr>
            <td class="studentName"><%:student.FullName%></td>
           <%
               foreach (PlaningType type in (IEnumerable<PlaningType>) ViewData["PlaningTypes"])
               {%>
                <%
                   foreach (
                       GlobalTask globalTask in
                           type.GlobalTasks.Where(gt => gt.QualificationLevel == level.Key && gt.DeletionDate == null).
                               OrderBy(gt => gt.StartDate))
                   {%>
                   <%-- <%if (globalTask.PrevCheckPoint != null)
                      { %>--%>
                        <td>
                            <%
                       Html.RenderAction("StudentsTasks", "Students",
                                         new {globalTaskId = globalTask.CheckPointId, studentId = student.UserId});%>
                        </td>
                   <%-- <%} %>--%>
                    <%--<td> --%>                   
                        <%
                       Html.RenderAction("Get", "GlobalTasksCompletions",
                                         new {globalTaskId = globalTask.CheckPointId, studentId = student.UserId});%>               
                    <%--</td>--%>
                <%
                   }%>
            <%
               }%>
            </tr> 
       <%
           }%>
       </table>
       </div>
   <%
       }%>